package com.example.defaultshop;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

public class XmlParser {
	public static Shop getShop(XmlPullParser parser) throws XmlPullParserException, IOException, NumberFormatException {
		parser.nextTag();
    	parser.require(XmlPullParser.START_TAG, XmlPullParser.NO_NAMESPACE, "Shop");
    	String name = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "name");
    	String city = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "city");
    	String zipcode = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "zipcode");
    	String address = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "address");
    	String hours = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "hours");
    	String orders = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "orders");
    	String delivery = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "delivery");
    	String deliveryText = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "deliverytext");
    	String smiley = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "smiley");
    	String closed = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "closed");
    	String picture = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "picture");
    	String hexColor = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "hexcolor");
    	int idx = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "idx"));
    	int deliverPrice = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "deliverprice"));
    	int deliverFree = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "deliverfree"));
    	int deliverMin = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "delivermin"));
    	int deliverPriceDefault = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "deliverpricedefault"));
    	int timeNormal = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "timenormal"));
    	int timeDeliver = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "timedeliver"));
    	
    	int version = 1;
    	try {
    		version = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "version"));
    	} catch (NumberFormatException e) { }
    	
    	boolean deliver = (Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "deliver")) == 1 ? true : false);
		return new Shop(idx, deliverPrice, deliverFree, deliverMin, deliverPriceDefault, timeNormal, timeDeliver, version, deliver, name, city, zipcode, address, hours, orders, delivery, deliveryText, smiley, closed, picture, hexColor);
	}
	
	public static Campaign getCampaign(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.nextTag();
    	parser.require(XmlPullParser.START_TAG, XmlPullParser.NO_NAMESPACE, "Campaign");
    	String header = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "header");
    	String picture = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "picture");
    	String message = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "message");
    	String obs = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "obs");
    	int idx = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "idx"));
		return new Campaign(idx, header, message, obs, picture);
	}
	
	public static int getBasketId(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.nextTag();
		parser.require(XmlPullParser.START_TAG, XmlPullParser.NO_NAMESPACE, "BasketId");
		int idx = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "basketId"));
		return idx;
	}
	
	public static ArrayList<Category> getCategories(XmlPullParser parser) throws XmlPullParserException, IOException {
		ArrayList<Category> categories = new ArrayList<Category>();
		
		parser.nextTag();
    	parser.require(XmlPullParser.START_TAG, null, "Categories");
    	
		while (parser.next() != XmlPullParser.END_DOCUMENT) {
    		if (parser.getEventType() != XmlPullParser.START_TAG)
    			continue;
    			
    		String tag = parser.getName();
    		if (tag.equals("Category"))
    			categories.add(parseCategory(parser));
    		tag = null;
		}
		return categories;
	}
	
	public static Category parseCategory(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "Category");
		String title = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "title");
		String extra = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "extra");
		int idx = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "idx"));
		return new Category(idx, title, extra);
	}
	
	public static ArrayList<Menu> getMenu(XmlPullParser parser) throws XmlPullParserException, IOException {
		ArrayList<Menu> menu = new ArrayList<Menu>();
		
		parser.nextTag();
    	parser.require(XmlPullParser.START_TAG, null, "Menus");
    	
    	while (parser.next() != XmlPullParser.END_DOCUMENT) {
    		if (parser.getEventType() != XmlPullParser.START_TAG)
    			continue;
    			
    		String tag = parser.getName();
    		if (tag.equals("Menu"))
    			menu.add(parseMenu(parser));
    		tag = null;
		}
		return menu;
	}
	
	public static Menu parseMenu(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "Menu");
		String name = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "name");
		String extra = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "extra");
		String obs = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "obs");
		String nrtxt = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "nrtxt");
		String bon = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "bon");
		String picture = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "picture");
		int idx = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "idx"));
		int price = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "price"));
		int minAntal = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "minantal"));
		int maxAntal = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "maxantal"));
		int categoryId = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "categoryId"));
    	boolean showPicture = Boolean.parseBoolean(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "showpicture"));
		return new Menu(idx, name, extra, obs, nrtxt, bon, price, minAntal, maxAntal, categoryId, picture, showPicture);
	}
	
	public static ArrayList<Mix> getMix(XmlPullParser parser) throws XmlPullParserException, IOException {
		ArrayList<Mix> mix = new ArrayList<Mix>();
		
		parser.nextTag();
    	parser.require(XmlPullParser.START_TAG, null, "Mixes");
    	
    	while (parser.next() != XmlPullParser.END_DOCUMENT) {
    		if (parser.getEventType() != XmlPullParser.START_TAG)
    			continue;
    		
    		String tag = parser.getName();
    		if (tag.equals("Mix")) {
    			mix.add(parseMix(parser, tag));
    		} else if (tag.equals("Group")) {
    			MixGroup g = parseGroup(parser);
    			mix.add(g);
    			mix.addAll(g.getMix());
    		} else if (tag.equals("Header")) {
    			MixHeader h = parseHeader(parser);
    			mix.add(h);
    			mix.addAll(h.getMix());
    		}
    		tag = null;
		}
		return mix;
	}
	
	public static Mix parseMix(XmlPullParser parser, String require) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, require);
		
		Mix m = new Mix();
		m.setName(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "name"));
		m.setText(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "text"));
		m.setIdx(Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "idx")));
		m.setType(Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "type")));
		
		try {
			int extra = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "extra"));
			m.setExtra(extra);
		} catch (IllegalArgumentException e) {
			m.setExtra(0);
		}
		
		// go through parser until we get to a end tag
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG)
				continue;
			
			String tag = parser.getName();
			if (tag.equals("Option") || tag.equals("HeaderOption"))
				m.addOption(parseOption(parser, tag));
			tag = null;
		}
		if (m.getType() != 3 && !require.equals("GroupMix"))
			m.setChoice(m.getOptions().get(0));
		return m;
	}
	
	public static Option parseOption(XmlPullParser parser, String tag) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, tag);
		Option o = new Option();
		o.setName(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "name"));
		o.setIdx(Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "idx")));
		o.setPrice(Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "price")));
		o.setSelectable(Boolean.parseBoolean(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "selectable")));
		parser.nextTag();
		return o;
	}
	
	public static MixGroup parseGroup(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "Group");
		
		MixGroup g = new MixGroup();
		g.setName(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "name"));
		g.setText(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "text"));
		g.setIdx(Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "idx")));
		g.setAmount(Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "amount")));
		
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG)
				continue;
			
			String tag = parser.getName();
			if (tag.equals("GroupMix")) {
				Mix m = parseMix(parser, tag);
				for (int i = 0; i < 21; i++)
					m.addOption(new Option("" + i, true, -1, 0));
				m.setChoice(m.getOptions().get(0));
				g.addMix(m);
				m.setGroup(g);
			}
			tag = null;
		}
		return g;
	}
	
	public static MixHeader parseHeader(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "Header");
		
		MixHeader h = new MixHeader();
		h.setName(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "name"));
		h.setText(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "text"));
		h.setIdx(Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "idx")));
		
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG)
				continue;
			
			String tag = parser.getName();
			if (tag.equals("HeaderMix"))
				h.addMix(parseMix(parser, tag));
			tag = null;
		}
		return h;
	}
	
	public static int getItemId(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.nextTag();
		parser.require(XmlPullParser.START_TAG, XmlPullParser.NO_NAMESPACE, "ItemId");
		int idx = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "itemId"));
		return idx;
	}
	
	public static ArrayList<Item> getBasket(XmlPullParser parser) throws XmlPullParserException, IOException {
		ArrayList<Item> basket = new ArrayList<Item>();
		parser.nextTag();
		parser.require(XmlPullParser.START_TAG, null, "Basket");
		
		while (parser.next() != XmlPullParser.END_DOCUMENT) {
    		if (parser.getEventType() != XmlPullParser.START_TAG)
    			continue;
			
			String tag = parser.getName();
			if (tag.equals("Item"))
				basket.add(parseBasketItem(parser));
			tag = null;
		}
		return basket;
	}
	
	public static Item parseBasketItem(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "Item");
		String name = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "name");
		String nrtxt = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "nrtxt");
		String choices = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "mix");
		String picture = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "picture");
		int idx = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "idx"));
		int price = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "price"));
		int amount = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "amount"));
		int menuId = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "menuId"));
		int categoryId = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "categoryId"));
    	boolean showPicture = Boolean.parseBoolean(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "showpicture"));
		return new Item(name, nrtxt, choices, picture, showPicture, idx, categoryId, menuId, price, amount);
	}
	
	public static boolean getDeleteResponse(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.nextTag();
		parser.require(XmlPullParser.START_TAG, null, "Success");
		boolean response = Boolean.parseBoolean(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "success"));
		return response;
	}
	
	public static int getOrderId(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.nextTag();
		parser.require(XmlPullParser.START_TAG, null, "OrderId");
		int idx = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "orderId"));
		return idx;
	}
	
	public static Status getOrderStatus(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.nextTag();
		parser.require(XmlPullParser.START_TAG, null, "Bestil-Let");
		String obs = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "obs");
		String time = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "time");
		String text = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "text");
		int status = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "status"));
		return new Status(time, text, obs, status);
	}
	
	public static ArrayList<Order> getOrders(XmlPullParser parser) throws XmlPullParserException, IOException {
		ArrayList<Order> orders = new ArrayList<Order>();
		parser.nextTag();
		parser.require(XmlPullParser.START_TAG, null, "Orders");
		
		while (parser.next() != XmlPullParser.END_DOCUMENT) {
    		if (parser.getEventType() != XmlPullParser.START_TAG)
    			continue;
			
			String tag = parser.getName();
			if (tag.equals("Order"))
				orders.add(parseOrder(parser));
			tag = null;
		}
		return orders;
	}
	
	public static Order parseOrder(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "Order");
		String name = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "name");
		String city = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "city");
		String date = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "date");
		int orderId = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "orderId"));
		int basketId = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "basketId"));
		int total = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "total"));
		return new Order(name, city, date, orderId, basketId, total);
	}
	
	public static ArrayList<Card> getCards(XmlPullParser parser) throws XmlPullParserException, IOException {
		ArrayList<Card> cards = new ArrayList<Card>();
		parser.nextTag();
		parser.require(XmlPullParser.START_TAG, null, "Cards");
		
		while (parser.next() != XmlPullParser.END_DOCUMENT) {
			if (parser.getEventType() != XmlPullParser.START_TAG)
				continue;
			
			String tag = parser.getName();
			if (tag.equals("Card"))
				cards.add(parseCard(parser));
			tag = null;
		}
		return cards;
	}
	
	public static Card parseCard(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "Card");
		String name = parser.getAttributeValue(1);
		int idx = Integer.parseInt(parser.getAttributeValue(0));
		int amount = Integer.parseInt(parser.getAttributeValue(2));
		return new Card(name, idx, amount);
	}
	
	public static ArrayList<Stamp> getStamps(XmlPullParser parser) throws XmlPullParserException, IOException {
		ArrayList<Stamp> stamps = new ArrayList<Stamp>();
		parser.nextTag();
		parser.require(XmlPullParser.START_TAG, null, "Stamps");
		
		while (parser.next() != XmlPullParser.END_DOCUMENT) {
			if (parser.getEventType() != XmlPullParser.START_TAG)
				continue;
			
			String tag = parser.getName();
			if (tag.equals("Stamp"))
				stamps.add(parseStamp(parser));
			tag = null;
		}
		return stamps;
	}
	
	public static Stamp parseStamp(XmlPullParser parser) throws XmlPullParserException, IOException {
		parser.require(XmlPullParser.START_TAG, null, "Stamp");
		int idx = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "idx"));
		int shopId = Integer.parseInt(parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "shopId"));
		String date = parser.getAttributeValue(XmlPullParser.NO_NAMESPACE, "date");
		return new Stamp(date, idx, shopId);
	}
}